package com.next

import groovy.transform.CompileStatic
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken
import org.springframework.security.authentication.dao.DaoAuthenticationProvider
import org.springframework.security.core.AuthenticationException
import org.springframework.security.core.userdetails.UserDetails

@CompileStatic
public class CustomAuthenticationProvider extends DaoAuthenticationProvider
{
    protected void additionalAuthenticationChecks(UserDetails userDetails,
        UsernamePasswordAuthenticationToken authentication) throws AuthenticationException
    {
        super.additionalAuthenticationChecks(userDetails, authentication)
        Object details = authentication.details

        def customWebAuthenticationDetails = details as CustomWebAuthenticationDetails
        def user = userDetails as CustomUserDetails
        //TODO
        if (user.fixedIp)
        {
            if (user.ip != customWebAuthenticationDetails.getRemoteAddr())
            {
                throw new CustomIpFixedException("User ip fiexed")
            }
        }
    }

}
